let Url = JSON.stringify(location.href);
let index = Url.indexOf("="); //传的值
let keyword = decodeURI(getParamsByUrl(location.href, "keyword")); //传的参数名

$(
    function() {
        console.log("keyword" + keyword);
        checkShopItemLogin();
        getData();


    }
)

function getParamsByUrl(url, name) {
    let params = url.substr(url.indexOf("=") + 1);
    return params;
}

function getData() {

    $.ajax({
        url: "/product/queryProductDetail",
        type: "get",
        data: {
            id: parseInt(keyword)
        },
        dataType: 'json',
        success: function(res) {
            console.log(res);
            console.log(res.pic.length);
            let html = template("shopItemTpl_samllpic", { data: res });
            $("#smallPic").html(html);
            console.log(html);

            //title
            let html1 = template("shopItemTpl_title", { data: res });
            $("#title").html(html1);

            let html2 = template("shopItemTpl_detail", { data: res });
            $("#datail").html(html2);

            let html3 = template("shopItemTpl_bigImg", { data: res });
            $("#bigImg").html(html3);

            let html4 = template("shopItemTpl_selectImg", { data: res });
            $("#select").html(html4);

            let html5 = template("shopItemTpl_price", { data: res });
            $("#price").html(html5);
        }
    })
}


function checkShopItemLogin() {
    let userInfo = checkLoginName();
    console.log("userInfo" + userInfo);
    let html = template("userTpl", { userInfo });
    $("#userName").html(html);

}


function addCart() {
    $("#amount").val();

    $.ajax({
        type: 'post',
        url: '/cart/addCart',
        data: {
            productId: keyword,
            num: parseInt($("#amount").val())
        },
        dataType: 'json',
        success: function(data) {
            location.href = "cart.html"
            console.log(data);
        }
    });
    //  location.reload();
}


function str() {
    var num = parseInt($("#amount").val());
    num--;
    if (num < 0) {
        num = 0;
    }
    console.log("num");
    $("#amount").val(parseInt(num));
    console.log("onclick-");
}

function add() {
    var num = parseInt($("#amount").val());
    num++;
    $("#amount").val(parseInt(num));
    console.log("onclick-");
}

function _Mouseout(i) {
    i = parseInt(i);
    $("#main_img").attr("src", $(".img_class")[i].src);
}


// $("#main_img").mouseover(function(e) {
//     var evt = window.event || e;

//     larger.style.display = "block";
//     shadow.style.display = "block";
//     var clientX = evt.clientX;
//     var clientY = evt.clientY;

//     var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
//     var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

//     var X = clientX + scrollLeft - chooseMarginL - shadowW / 2;
//     var Y = clientY + scrollTop - chooseMarginT - shadowH / 2;

//     if (X <= 0) {
//         X = 0;
//     }
//     if (X >= maxX) {
//         X = maxX;
//     }
//     if (Y <= 0) {
//         Y = 0;
//     }
//     if (Y >= maxY) {
//         Y = maxY;
//     }
//     //        防止遮罩层粘滞，跟随鼠标一起滑出大图位置
//     var bigX = X * bigW / contentW;
//     var bigY = Y * bigH / contentH;
//     //  bigX / bigW = X / contentW,主图和遮罩层之间存在两倍关系，放大图和原图之间也有两倍关系
//     shadow.style.left = X + "px";
//     shadow.style.top = Y + "px";

//     big.style.left = -bigX + "px";
//     big.style.top = -bigY + "px";

// })